package {{package}};

{{#imports}}import {{import}};
{{/imports}}

import com.fasterxml.jackson.core.type.TypeReference;
import io.vertx.core.json.jackson.DatabindCodec;
import io.vertx.ext.web.openapi.RouterBuilder;
import io.vertx.ext.web.validation.RequestParameters;
import io.vertx.ext.web.validation.RequestParameter;
import io.vertx.ext.web.validation.ValidationHandler;
import io.vertx.ext.web.RoutingContext;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class {{classname}}Handler {

    private static final Logger logger = LoggerFactory.getLogger({{classname}}Handler.class);

    private final {{classname}} api;

    public {{classname}}Handler({{classname}} api) {
        this.api = api;
    }

    @Deprecated
    public {{classname}}Handler() {
        this(new {{classname}}Impl());
    }

    public void mount(RouterBuilder builder) {
{{#operations}}
{{#operation}}
        builder.operation("{{operationId}}").handler(this::{{operationId}});
{{/operation}}
{{/operations}}
    }

{{#operations}}
{{#operation}}
    private void {{operationId}}(RoutingContext routingContext) {
        logger.info("{{operationId}}()");

        // Param extraction
        RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);

{{#allParams}}{{>headerParams}}{{>pathParams}}{{>queryParams}}{{>formParams}}{{>bodyParams}}{{/allParams}}
{{#allParams}}
        logger.debug("Parameter {{paramName}} is {}", {{paramName}});
{{/allParams}}

        api.{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}})
            .onSuccess(apiResponse -> {
                routingContext.response().setStatusCode(apiResponse.getStatusCode());
                if (apiResponse.hasData()) {
                    routingContext.json(apiResponse.getData());
                } else {
                    routingContext.response().end();
                }
            })
            .onFailure(routingContext::fail);
    }

{{/operation}}
{{/operations}}
}
